<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright 2002 - 2011 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: solar_index_panel.php
| Author:   Stewart Wilkinson G0LGS
| Version:  0.2
| Date:     25/01/2011
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
if (!defined("IN_FUSION")) { die("Access Denied"); }

// Panel Info from Php-Fusion
global $p_data;

// Check if locale file is available matching the current site locale setting.
if (file_exists(INFUSIONS."solar_index_panel/locale/".$settings['locale'].".php")) {

	// Load the locale file matching the current site locale setting.
	include INFUSIONS."solar_index_panel/locale/".$settings['locale'].".php";
} else {
	// Load the infusion's default locale file.
	include INFUSIONS."solar_index_panel/locale/English.php";
}

require_once INFUSIONS."solar_index_panel/includes/functions.php";

// Location of Geophysical Alert Message 'wwv.txt'
$wwvurl="http://www.swpc.noaa.gov/ftpdir/latest/wwv.txt";

// Buffer
$wwvdata = "";

$cache_file = INFUSIONS."solar_index_panel/cache/".md5($wwvurl);
$timediff = @(time() - filemtime($cache_file));

if( $timediff < 900 ){
	// Current
	$fc = @ fopen("$cache_file", "rb");
	if( $fc ){
		while( !feof($fc) ){
			$wwvdata .= fread($fc, 1024);
		}
		fclose( $fc );
	}
}else{
	// Old - Create new Cache
	$wwvdata = GetURL( $wwvurl );
	if( $wwvdata ){
		$fc = @ fopen("$cache_file", "w+");
		if( $fc ){
			fwrite( $fc, $wwvdata, strlen($wwvdata) );
			fclose( $fc );
		}
	}
}

// Issued Date/Time
preg_match( "':Issued: (.*)'i", $wwvdata, $out);
if( $out[1] ){
	$informat = '%Y %b %d %H%M %Z';
	if( ! ($ftime = strptime($out[1],$informat)) === false) {
		$wwvdate = mktime(
		$ftime['tm_hour'], 
		$ftime['tm_min'], 
		0, 
		$ftime['tm_mon']+1, 
		$ftime['tm_mday'],
		$ftime['tm_year']+1900,
		0 ); 
	}
}


// Solar flux / A-Index.
preg_match( "'Solar flux (\d{1,}) .* A-index (\d{1,})'i", $wwvdata, $out);
if( $out[1] ){
	$SF = $out[1];
}else{
	$SF = 0;
}
if( $out[2] ){
	$AI = $out[2];
}else{
	$AI = 0;
}

// K-index
preg_match( "'.* K-index at (.*) on (.*) was (\d{1,}) \(.*\).'i", $wwvdata, $out);
if( $out[3] ){
	$KI = $out[3];
}else{
	$KI = 0;
}

if( ($p_data['panel_side'] == 1) or ($p_data['panel_side'] == 4 ) ){
	openside($locale['sol_title'], true, "on");
}else{
	opentable($locale['sol_title']);
}

echo "<table width='100%' cellpadding='2' cellspacing='0' border='0'>\n";

$KI=2;

if( $KI < 4 ){
	$style="background:black;color:#00ff00;font-size: 12px;";
}elseif( $KI == 4 ){
	$style="background:black;color:#ffff00;font-size: 12px;";
}else{
	$style="background:black;color:#ff0000;font-size: 12px;";
}

echo "<tr><th style='".$style."'>";
echo "<a style='".$style."' title='".$locale['sol_001']."'>SFI:".$SF."</a> ";
echo "<a style='".$style."' title='".$locale['sol_002']."'>A:".$AI."</a> ";
echo "<a style='".$style."' title='".$locale['sol_003']."'>K:".$KI."</a>";
echo "</th></tr>\n";

// Geomagnetic Storm Level
if( $KI == 4 ){
	echo "<tr><th style='".$style."'>";
	echo $locale['sol_010'];
	echo "</th></tr>\n";
}elseif( $KI >= 5 ){
	echo "<tr><th style='".$style."'>";
	echo $locale['sol_011']." 'G".($KI-4)."'";
	echo "</th></tr>\n";
}

if( $wwvdate ){
	echo "<tr><th style='font-size: 9px;'>";
	echo $locale['sol_100'] . date($locale['sol_101'], $wwvdate);
	echo "</th></tr>\n";
}

//echo "<tr><td><hr width='75%' /></td></tr>\n";
echo "<tr><td>&nbsp;</td></tr>\n";

echo "<tr><th>".$locale['sol_004a']."</th></tr>";
echo "<tr><td align='center'><a href='http://www.swpc.noaa.gov/rt_plots/xray_1m.html' target='_blank' title='".$locale['sol_004']."'>";
echo "<img src='http://www.swpc.noaa.gov/rt_plots/Xray_1m_sm.gif' width='115' height='86' border='1' alt='".$locale['sol_004']."' /></a>";
echo "</td></tr>\n";

//echo "<tr><td><hr width='75%' /></td></tr>\n";
echo "<tr><td>&nbsp;</td></tr>\n";

echo "<tr><th>".$locale['sol_005a']."</th></tr>";
echo "<tr><td align='center'><a href='http://www.swpc.noaa.gov/rt_plots/kp_3d.html' target='_blank' title='".$locale['sol_005']."'>";
echo "<img src='http://www.swpc.noaa.gov/rt_plots/Kp_sm.gif' width='115' height='86' border='1' alt='".$locale['sol_005']."' /></a>";
echo "</td></tr>\n";

//echo "<tr><td><hr width='75%' /></td></tr>\n";
echo "<tr><td>&nbsp;</td></tr>\n";

echo "<tr><th>".$locale['sol_110']."<a href='http://www.swpc.noaa.gov/' target='_blank' ";
echo "title='The Space Weather Prediction Center, Boulder, CO, National Oceanic and Atmospheric Administration (NOAA), US Dept. of Commerce'>SWPC</a></th></tr>\n";

echo "<tr><td>&nbsp;</td></tr>\n";
echo "</table>\n";

echo "<table width='100%' cellpadding='1' cellspacing='0' border='0'>\n";
echo "<tr><td width='100%' align='center'><hr size='1' noshade='noshade' /></td></tr>\n";
echo "<tr><td width='100%' align='center' class='small'>". $locale['sol_name'];
echo "<a title='V".$locale['sol_version']." (".$locale['sol_verdate'].")'> V".$locale['sol_version']."</a><br />\n";
echo "&copy; 2011 by <a href='http://www.g0lgs.co.uk/downloads.php'>G0LGS</a></td></tr>\n";
echo "</table>\n";

if( ($p_data['panel_side'] == 1) or ($p_data['panel_side'] == 4 ) ){
	closeside();
}else{
	closetable();
}

?>
